from fastapi import APIRouter, HTTPException
from web.schemas import PredictRequest, PredictResponse
from web.service import predict_service

# 创建路由
predict_router = APIRouter()


# 预测接口
@predict_router.post('/predict')
def predict(request: PredictRequest) -> PredictResponse:
    try:
        text = request.text.strip()
        if not text:
            raise HTTPException(status_code=400, detail='输入文本不能为空')

        pred_id, pred_label = predict_service(text)

        return PredictResponse(text=text, pred_id=pred_id, pred_label=pred_label)

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
